home *** CD-ROM | disk | FTP | other *** search
/ BBS in a Box 4 / BBS in a Box - Macintosh - Volume IV (January 1992) (BBS in a Box).iso / Files / Prog / S / SerDemo.p < prev    next >
Encoding:
Text File  |  1989-01-06  |  5.3 KB  |  153 lines  |  [TEXT/TPAS]

  1. { A demonstration of the Serial Drivers.  Written for Turbo Pascal, but    }
  2. { should be easily transported to other Pascals.                  01/06/88 }
  3. { Written by Michael South, Nautilus Data Services, (509) 545-5424.        }
  4.  
  5. PROGRAM SerDemo;
  6. {$T APPL????}               { Set program file type and creator }
  7.  
  8. USES Memtypes,QuickDraw,OSIntf,ToolIntf,PackIntf;
  9.  
  10. CONST
  11.     AscCR       = 13;       { ASCII value of a carriage return }
  12.     AscXON      = 17;       { ASCII for DC1 (Control-Q) }
  13.     AscXOFF     = 19;       { ASCII for DC3 (Control-S) }
  14.  
  15. VAR modemIn     : INTEGER;  { Driver reference # for modem port }
  16.     theEvent    : EventRecord;
  17.     dummyB      : BOOLEAN;
  18.  
  19. { Open the modem port for input }
  20.  
  21. PROCEDURE Init;
  22. VAR handshake   : SerShk;
  23. BEGIN
  24.     IF RAMSDOpen(sPortA)<>noErr { For compatibility w/ older Macs }
  25.         THEN WriteLn('Cannot open RAM driver');
  26.     IF OpenDriver('.AIn',modemIn)<>noErr
  27.         THEN WriteLn('Cannot open modem port');
  28.     IF SerReset(modemIn, baud1200+stop10+noParity+data8)<>noErr
  29.         THEN WriteLn('Cannot set baud/data format');
  30.     WITH handshake DO
  31.         BEGIN
  32.             fXon    := 0;   { No output flow control }
  33.             xOn     := Char(AscXOn);   { Xon character }
  34.             xOff    := Char(AscXOff);  { Xoff character }
  35.             errs    := 0;   { Ignore framing/overrun errors }
  36.             evts    := 0;   { No device driver events created }
  37.             fInX    := 1    { Enable Xon/Xoff input flow control }
  38.         END;
  39.     IF SerHShake(modemIn, handshake)<>noErr
  40.         THEN WriteLn('Cannot set handshaking')
  41. END;
  42.  
  43. { ========== First method of reading a line from the serial port ========== }
  44.  
  45. PROCEDURE ReadStr1;
  46. VAR MyString    : STR255;
  47.     DoneWithLine: BOOLEAN;  { When true, a complete line has been recieved }
  48.     AChar       : BOOLEAN;  { Actually, a 1-byte char }
  49.     NumRead     : LONGINT;  { How many characters to read/were read }
  50. BEGIN
  51.     MyString:='';
  52.     DoneWithLine := FALSE;
  53.  
  54.     { Keep getting characters until CR, or 255 characters have been read, }
  55.     { or the mouse button is pushed.                                      }
  56.  
  57.     WHILE (NOT DoneWithLine) AND (NOT Button) DO
  58.         BEGIN
  59.             IF SerGetBuf(modemIn, NumRead)<>noErr
  60.                 THEN WriteLn('Cannot get # of chars in buffer');
  61.             IF NumRead>0
  62.                 THEN
  63.                     BEGIN
  64.                         NumRead := 1;   { Only get 1 char }
  65.                         IF FSRead(modemIn, NumRead, @AChar)<>noErr
  66.                             THEN WriteLn('Cannot read modem port');
  67.                         IF NumRead>0
  68.                             THEN
  69.                                 BEGIN
  70.                                     MyString :=
  71.                                         Concat(MyString, Char(Ord(AChar)));
  72.                                     IF (Ord(AChar)=AscCR) OR
  73.                                        (Length(MyString)=255)
  74.                                         THEN DoneWithLine:=TRUE
  75.                                 END
  76.                     END;
  77.         END;
  78.  
  79.     IF DoneWithLine THEN WriteLn(MyString)
  80.  
  81. END;
  82.  
  83. { ========= Second method of reading a line from the serial port ========== }
  84.  
  85. PROCEDURE ReadStr2;
  86.  
  87. VAR MyString    : STR255;
  88.     NumRead     : LONGINT;  { How many characters to read/were read }
  89.     TempBPtr    : ^BOOLEAN; { Used when forcing length of MyString }
  90.  
  91.     { The following field is used to move an integer into an unsigned byte,}
  92.     { without Pascal doing unwanted sign extension.                        }
  93.     Int2UByte  : RECORD
  94.                     CASE INTEGER OF
  95.                         0   : (int  : INTEGER);
  96.                         1   : (dumm : BOOLEAN;
  97.                                ubyt : BOOLEAN)
  98.                     END;
  99.  
  100.     { NOTE: The type BYTE (an unsigned byte) is actually stored as a *word* }
  101.     { (unless it's part of a packed structure).  BOOLEAN is the only        }
  102.     { unpacked data type that is stored as a single byte.                   }
  103.  
  104. BEGIN
  105.  
  106.     { Freeze computer until 255 characters have been read }
  107.  
  108.         NumRead := 255;   { Get 255 char }
  109.         IF FSRead(modemIn, NumRead, POINTER(ORD4(@MyString)+1))<>noErr
  110.             THEN WriteLn('Cannot read modem port');
  111.  
  112.     { Force the length of MyString }
  113.  
  114.         Int2UByte.int := NumRead;
  115.         TempBPtr := POINTER(ORD4(@MyString));  { Points to length byte }
  116.         TempBPtr^ := Int2UByte.ubyt; { Have Pascal move a single byte }
  117.  
  118.     WriteLn(Button, MyString)
  119.  
  120. END;
  121.  
  122. PROCEDURE WrapUp;
  123. BEGIN
  124.     IF CloseDriver(modemIn)<>noErr THEN WriteLn('Cannot close');
  125.     RAMSDClose(sPortA)
  126. END;
  127.  
  128. BEGIN
  129.     Init;
  130.     REPEAT
  131.         BEGIN
  132.             ReadStr1;
  133.            {ReadStr2;}
  134.             dummyB:=GetNextEvent(everyEvent,theEvent)
  135.         END; 
  136.     UNTIL theEvent.what=mouseDown;
  137.     WrapUp
  138. END.
  139.  
  140. { Caveats:
  141. This program works OK at 1200 baud. However, as the baud rate increases the
  142. Mac spends more and more time drawing the characters on the screen. The
  143. limit, using Pascal and simple WriteLns, is about 1200 baud.
  144.  
  145. At speeds over 1200 baud, it is important that the sending computer and the
  146. Mac use some type of "flow control". The most common is Xon-Xoff. It allows
  147. one party to tell the other to "wait a minute" (x-off) and "go ahead" (x-on).
  148. The above routine is configured for X-on/x-off.
  149.  
  150. If flow control is not used or working, then a lot of garbage will be
  151. recieved.
  152. }
  153.